package top.frsf.dashboard.config;

import lombok.Data;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Broker 运行状态
 *
 * @author He Kuntao
 * @since 2025/6/3 14:28
 */
@Data
@Configuration
public class BrokerStats {
    /**
     * 启动时间
     */
    private final long startTime = System.currentTimeMillis();
    /**
     * 消息接收数量
     */
    private final AtomicLong messagesReceived = new AtomicLong(0);
    /**
     * 消息发送数量
     */
    private final AtomicLong messagesSent = new AtomicLong(0);


    /**
     * 获取 Broker 已运行时间，格式为 xx天xx小时xx分钟
     *
     * @return 运行时间字符串
     */
    public String getUptime() {
        Instant startInstant = Instant.ofEpochMilli(startTime);
        Duration duration = Duration.between(startInstant, Instant.now());

        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;

        return days + "天" + hours + "小时" + minutes + "分钟";
    }

    /**
     * 增加消息接收数量
     */
    public void incrementMessagesReceived() {
        messagesReceived.incrementAndGet();
    }

    /**
     * 增加消息发送数量
     */
    public void incrementMessagesSent() {
        messagesSent.incrementAndGet();
    }

}
